package org.jash.mvp2206.news;

import android.content.Context;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.Glide;
import com.google.android.material.search.SearchView;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import org.jash.mvp2206.R;
import org.jash.mvp2206.entity.AllCategory;
import org.jash.mvp2206.entity.NewsPage;
import org.jash.mvp2206.entity.TopHot;
import org.jash.mvp2206.mvp.BaseFragment;

import java.util.List;
import java.util.stream.Collectors;

public class NewsCategoryFragment extends BaseFragment<Construct.INewsCategoryView, NewsCategoryPresenter> implements Construct.INewsCategoryView {
    private TabLayout tab;
    private Banner banner;
    private ViewPager2 pager;
    private CategoryAdapter adapter;
    private RNewsAdapter newsAdapter;
    private RecyclerView result;
    private List<TopHot.DataBean> news;
    private SearchView search;

    @Override
    protected int layoutId() {
        return R.layout.activity_news_category;
    }

    @Override
    protected void initData() {
        presenter = new NewsCategoryPresenter();
        presenter.getAllCategories();
        presenter.getTopHot();
    }

    @Override
    protected void initView() {
        tab = findViewById(R.id.tab);
        banner = findViewById(R.id.banner);
        pager = findViewById(R.id.pager);
        result = findViewById(R.id.search_result);
        search = findViewById(R.id.search);
        newsAdapter = new RNewsAdapter();
        result.setAdapter(newsAdapter);
        adapter = new CategoryAdapter(this);
        pager.setAdapter(adapter);
        new TabLayoutMediator(tab, pager, (tab1, position) -> tab1.setText(adapter.getTitle(position))).attach();
        banner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(imageView)
                        .load(path)
                        .into(imageView);
            }
        }).setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        search.getEditText().setOnEditorActionListener((v, actionId, event) -> {
            String key = v.getText().toString();
            newsAdapter.clear();
            List<NewsPage.DataBean.RecordsBean> list = news.stream().filter(dataBean -> dataBean.getTitle().contains(key)).map(dataBean -> {
                NewsPage.DataBean.RecordsBean bean = new NewsPage.DataBean.RecordsBean();
                bean.setId(dataBean.getId());
                bean.setTitle(dataBean.getTitle());
                bean.setCreateTime(dataBean.getCreateTime());
                bean.setImgurl(dataBean.getImgurl());
                return bean;
            }).collect(Collectors.toList());
            newsAdapter.addAll(list);
            return true;
        });
    }

    @Override
    public void allCategoriesSuccess(List<AllCategory.DataBean> categories) {
        adapter.addAll(categories);
    }

    @Override
    public void topHotSuccess(List<TopHot.DataBean> news) {
        this.news = news;
        banner.update(news.stream().map(TopHot.DataBean::getImgurl).collect(Collectors.toList()),
                news.stream().map(TopHot.DataBean::getTitle).collect(Collectors.toList()));
    }

    @Override
    public void failure(Throwable t) {
        Toast.makeText(requireContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}